import 'package:flare_flutter/flare_actor.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:sui_shou_pai/module/color.dart';
import 'package:sui_shou_pai/module/effect.dart';

import 'code_input_cell/code_input_row.dart';
import 'new_password.dart';

class CodePage extends StatefulWidget {
  final String phoneNum;

  CodePage({Key key, this.phoneNum}) : super(key: key);

  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return CodePageState();
  }
}

class CodePageState extends State<CodePage> {
  TextEditingController _controller = TextEditingController();
  String animationType = "idle";
  String _code = '';
  var _length = 6; //验证码长度，输入框框的个数
  var _type = CodeInputType.squareBox;
  final phoneFocusNode = FocusNode();

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
        child: Scaffold(
          body: Stack(
            children: <Widget>[
              Container(
                height: 300,
                color: greyF3F3F3,
                padding: EdgeInsets.only(top: 60, bottom: 30),
                child: FlareActor('assets/Teddy.flr',
                    shouldClip: false,
                    alignment: Alignment.bottomCenter,
                    fit: BoxFit.contain,
                    animation: animationType),
              ),
              Container(
                width: MediaQuery.of(context).size.width,
                margin: EdgeInsets.only(top: 270),
                decoration: BoxDecoration(
                  borderRadius: borderRadiusOnlyLeftTop24(),
                  color: Colors.white,
                ),
                child: ListView(
                  padding: EdgeInsets.symmetric(vertical: 36, horizontal: 44),
                  children: <Widget>[
                    Padding(
                      padding: EdgeInsets.only(bottom: 16),
                      child: Text(
                        '输入验证码',
                        style: TextStyle(
                            fontSize: 40, fontWeight: FontWeight.bold),
                      ),
                    ),
                    Padding(
                      padding: EdgeInsets.only(bottom: 24),
                      child: Text(
                        '短信验证码已发送至 ${widget.phoneNum}',
                        style: TextStyle(
                            fontSize: 18,
                            fontWeight: FontWeight.normal,
                            color: Colors.grey),
                      ),
                    ),
                    Padding(
                      padding: EdgeInsets.symmetric(vertical: 10),
                      child: Container(
                        child: Stack(
                          children: [
                            CodeInputRow(
                                code: _code, length: _length, type: _type),
                            Opacity(
                              opacity: 0,
                              child: TextField(
                                //只能输入字母与数字
                                // inputFormatters: [
                                //   WhitelistingTextInputFormatter(
                                //       RegExp("[a-z,0-9,A-Z]"))
                                // ],
                                autofocus: true,
                                keyboardType: TextInputType.number,
                                controller: _controller,
                                onChanged: (String str) {
                                  _code = str;
                                  setState(() {});
                                  if (str.length == _length) {
                                    if (_code == "654321") {
                                      Navigator.push(
                                          context,
                                          new MaterialPageRoute(
                                              builder: (context) =>
                                                  NewPasswordPage()));
                                    } else {
                                      Fluttertoast.showToast(msg: "验证码错误!");
                                    }
                                    _controller.text = '';
                                    _code = '';
                                  }
                                },
                              ),
                            )
                          ],
                        ),
                      ),
                    ),
                  ],
                ),
              ),
            ],
          ),
        ),
        onTap: () {
          hindKeyBoarder();
        });
  }

  void hindKeyBoarder() {
    //输入框失去焦点
    phoneFocusNode.unfocus();
    //隐藏键盘
    SystemChannels.textInput.invokeMethod('TextInput.hide');
  }
}
